#ifndef __DB_UTILS_5R5DC1NB
#define __DB_UTILS_5R5DC1NB

#include <sys/wait.h>
#include <pthread.h>

#include "libmdm.h"
#include "cms_msg.h"
#include "cms_log.h"
#include "cms_common.h"
#include "cms_event.h"
#include "cms_mdm.h"
#include "cmsXml_object.h"
#include "cms_utils.h"


#define CMS_DB_SAVE					(1 << 0)
#define CMS_DB_APPLY				(1 << 1)
#define CMS_DB_OBJECT_CHG_NOTIFY  	(1 << 2)
#define CMS_NO_NEED_REPLY			(1 << 3)

/*define for trans cms value type to cwmp value type*/
enum cwmp_data_type {
	CWMP_DATA_TYPE_UNKNOWN,
	CWMP_DATA_TYPE_OBJECT,
	CWMP_DATA_TYPE_MULTIPLE,
	CWMP_DATA_TYPE_STRING,
	CWMP_DATA_TYPE_INT,
	CWMP_DATA_TYPE_UNSIGNED_INT,
	CWMP_DATA_TYPE_BOOLEAN,
	CWMP_DATA_TYPE_DATETIME,
	CWMP_DATA_TYPE_BASE64
};

typedef struct cms_objChgList_s{
	struct cms_objChgList_s *prev;
	struct cms_objChgList_s *next;
	uint32_t objId;
	uint64_t objIA;
	uint32_t objOffset;
	uint32_t objSize;
	uint32_t flag;
	void *oldObject;
	void *newObject;
}cms_objChgList_t;


typedef struct cms_dbQueue_s{
	cmsXmlCb_t *xmlShmCb;
	cmsMsgCb_t *pMsg;
	int curObjId;
	cmsoffset_t curObjOffset;
	uint64_t curObjIA;
	char *curObjName;
	uint8_t	recordObjFlag;
	cms_objChgList_t *objChgList;
}cms_dbQueue_t;


typedef struct cmsDbContext_s{
	uint32_t moduleId;
	cmsXmlCb_t *pXmlCb;
	//cms_dbQueue_t *dbQ;
}cmsDbContext_t;

#ifdef DBUS_OBJECT_NOTIFY
typedef struct cms_dbusNotify_s{
	struct cms_dbusNotify_s *prev;
	struct cms_dbusNotify_s *next;
	cmsoffset_t objOff;
	char paramName[64];
}cms_dbusNotify_t;	
#endif


int cmsDb_init();
int cmsDb_addObjByOID(cms_dbQueue_t *dbQ, cmsMsg_action_t *pAction, cmsBoolean taskNotifyFlag);
int cmsDb_addObjByPath(cms_dbQueue_t *dbQ, cmsMsg_action_t *pAction, cmsBoolean taskNotifyFlag);
int cmsDb_delObjByOID(cms_dbQueue_t *dbQ, cmsMsg_action_t *pAction, cmsBoolean taskNotifyFlag);
int cmsDb_delObjByPath(cms_dbQueue_t *dbQ, cmsMsg_action_t *pAction, cmsBoolean taskNotifyFlag);
int cmsDb_getObj(cms_dbQueue_t *dbQ, cmsMsg_action_t *pAction);
int cmsDb_selObjByName(cms_dbQueue_t *dbQ, cmsMsg_action_t *pAction);
int cmsDb_selObjById(cms_dbQueue_t *dbQ, cmsMsg_action_t *pAction);
int cmsDb_selObjByOffset(cms_dbQueue_t *dbQ, cmsMsg_action_t *pAction);
int cmsDb_getParameter(cms_dbQueue_t *dbQ, cmsMsg_action_t *pAction);
int cmsDb_setParameter(cms_dbQueue_t *dbQ, cmsMsg_action_t *pAction, cmsBoolean taskNotifyFlag, cmsBoolean cwmpNotifyFlag);
int cmsDb_getParaAttr(cms_dbQueue_t *dbQ, cmsMsg_action_t *pAction);
int cmsDb_setParaAttr(cms_dbQueue_t *dbQ, cmsMsg_action_t *pAction);
int cmsDb_handleApply(cms_dbQueue_t *dbQ, cmsMsg_header_t *msgHeader);
int cmsDb_restoreDefault(cms_dbQueue_t *dbQ, cmsMsg_action_t *pAction);
int cmsDb_restoreConf(cms_dbQueue_t *dbQ, cmsMsg_action_t *pAction);
int cmsDb_objValueRestore(cms_dbQueue_t *dbQ, cmsMsg_action_t *pAction);
int cmsDb_save(cms_dbQueue_t *dbQ);
int cmsDb_recordObjChg(cms_dbQueue_t *dbQ,  cmsXmlCb_t *xmlShmCb, void *object, uint32_t flag);
void cmsDb_dbusNotify(cms_dbQueue_t *dbQ);


#endif /* end of include guard: __DB_UTILS_5R5DC1NB */
